package com.damocode.starter.yx.service.impl;

import com.damocode.starter.httpclient.domain.HttpResponse;
import com.damocode.starter.httpclient.exceptions.ClientException;
import com.damocode.starter.httpclient.helper.HttpClientHelper;
import com.damocode.starter.yx.autoconfigure.IMProperties;
import com.damocode.starter.yx.domain.HostType;
import com.damocode.starter.yx.domain.MsgType;
import com.damocode.starter.yx.http.YxMsgRequest;
import com.damocode.starter.yx.service.IIMMsgService;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class IMMsgService implements IIMMsgService {

    private IMProperties imProperties;

    private HttpClientHelper httpClientHelper;

    public IMMsgService(IMProperties imProperties,HttpClientHelper httpClientHelper){
        this.imProperties = imProperties;
        this.httpClientHelper = httpClientHelper;
    }

    public String sendMsg(String from, String ope, String to, String message) {
        String url = String.format(HostType.MSG.getStrType(), "sendMsg.action");
        try {
            YxMsgRequest yxMsgRequest = new YxMsgRequest(url,imProperties.getAppKey(),imProperties.getAppSecret());
            yxMsgRequest.setFrom(from);
            yxMsgRequest.setTo(to);
            yxMsgRequest.setType("0");
            yxMsgRequest.setOpe(ope);
            yxMsgRequest.setMessage(message);
            HttpResponse httpResponse = httpClientHelper.syncInvoke(yxMsgRequest);
            return httpResponse.getResponseText();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendMsg(String from, String ope, String to, MsgType msgType, String body) {
        String url = String.format(HostType.MSG.getStrType(), "sendMsg.action");
        try {
            YxMsgRequest yxMsgRequest = new YxMsgRequest(url,imProperties.getAppKey(),imProperties.getAppSecret());
            yxMsgRequest.setFrom(from);
            yxMsgRequest.setTo(to);
            yxMsgRequest.setType(msgType.getType());
            yxMsgRequest.setOpe(ope);
            yxMsgRequest.setBody(body);
            HttpResponse httpResponse = httpClientHelper.syncInvoke(yxMsgRequest);
            return httpResponse.getResponseText();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendBatchMsg(String fromAccid, String toAccids, String message) {
        String url = String.format(HostType.MSG.getStrType(), "sendBatchMsg.action");
        try {
            YxMsgRequest yxMsgRequest = new YxMsgRequest(url,imProperties.getAppKey(),imProperties.getAppSecret());
            yxMsgRequest.setFromAccid(fromAccid);
            yxMsgRequest.setToAccids(toAccids);
            yxMsgRequest.setType(MsgType.TEXT.getType());
            yxMsgRequest.setMessage(message);
            HttpResponse httpResponse = httpClientHelper.syncInvoke(yxMsgRequest);
            return httpResponse.getResponseText();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendBatchMsg(String fromAccid, String toAccids, MsgType msgType, String body) {
        String url = String.format(HostType.MSG.getStrType(), "sendBatchMsg.action");
        try {
            YxMsgRequest yxMsgRequest = new YxMsgRequest(url,imProperties.getAppKey(),imProperties.getAppSecret());
            yxMsgRequest.setFromAccid(fromAccid);
            yxMsgRequest.setToAccids(toAccids);
            yxMsgRequest.setType(msgType.getType());
            yxMsgRequest.setBody(body);
            HttpResponse httpResponse = httpClientHelper.syncInvoke(yxMsgRequest);
            return httpResponse.getResponseText();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendAttachMsg(String from, String to, int msgtype, String attach) {
        String url = String.format(HostType.MSG.getStrType(), "sendAttachMsg.action");
        try {
            YxMsgRequest yxMsgRequest = new YxMsgRequest(url,imProperties.getAppKey(),imProperties.getAppSecret());
            yxMsgRequest.setFrom(from);
            yxMsgRequest.setMsgtype(msgtype);
            yxMsgRequest.setTo(to);
            yxMsgRequest.setAttach(attach);
            HttpResponse httpResponse = httpClientHelper.syncInvoke(yxMsgRequest);
            return httpResponse.getResponseText();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
